package com.robaone.html.gwt;

import java.util.Vector;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;

public class NodeFinder {
	Vector<Element> v = null;
	private String attrib;
	public NodeFinder(String attribute){
		this.attrib = attribute;
	}
	public Element[] findNodesbyPattern(String pattern){
		Element[] retval = null;
		v = new Vector<Element>();
		Element body = Document.get().getBody();
		
		
		this.findChild(body,pattern);
		retval = v.toArray(new Element[0]);
		return retval;
	}
	private void findChild(Element node, String pattern) {
		String id = node.getAttribute(this.attrib);
		if(this.matches(id,pattern)){
			this.v.add(node);
		}
		
		for(int i = 0; i < com.google.gwt.user.client.DOM.getChildCount((com.google.gwt.user.client.Element) node);i++){
			Element child = com.google.gwt.user.client.DOM.getChild((com.google.gwt.user.client.Element) node, i);
			this.findChild(child, pattern);
		}
	}
	private boolean matches(String id, String pattern) {
		boolean retval = id.matches(pattern);
		return retval;
	}
}
